#!/bin/bash
#功能相关
get_key() {
    if [ $REQUEST_METHOD = "POST" ]; then
        #使用cat或者tee接受post的方法

        query_string=$(tee)

    else
        query_string=$QUERY_STRING
    fi

    echo $query_string | sed 's/&/\n/g' | grep -w "$1" | awk -F '=' '{print $2}'

}
post_key() {
    echo $1 | sed 's/&/\n/g' | grep -w "$2" | awk -F '=' '{print $2}'

}

getGroup() {
    file=$(mktemp /tmp/getGroup.XXXXXXXXXX)

    select=""
    mysql -hcm-mysql -uroot -p123.com cm-shell -e "select Name from  hostgroup" | sed -e '1d' >$file
    while read g; do
        select+="<option name="group">${g}</option>"
    done <$file
    Option=$select
    # rm -rf $file

}
exec_tasks() {
    cd /tmp/
    file=$(mktemp /tmp/task.XXXXXXXXXX)
    directory=$1
    group=$2
    getData Callback 'select * from host'
    Time=$(date +"%Y-%m-%e-%R")
    screen -S $file -m -d bash -c "cd  ../task/$template/ ; make deploy >/tmp/$file"
    sql="insert task set task=\"$file\",template=\"$template\",\`group\`=\"$group\",\`time\`=\"$Time\",\`content\`=\"$content\";"
    TaskHanle $sql

}

getTempate() {
    select=""
    for t in $(ls -l task | awk '{print $9}'); do
        select+="<option name="group">${t}</option>"

    done
    templates=$select

}

getTable() {

    trd_temp=""
    mysql -hcm-mysql -uroot -p123.com cm-shell -e "select * from template" | sed -e '1d' | grep -v "Warning" >template.txt
    i=0
    while read t; do

        trd_temp+="<tr><td>$(echo $t | awk '{print $1}')</td><td>$(echo $t | awk '{print $2}')</td><td>$(echo $t | awk '{print $3}')</td><td>$(echo $t | awk '{print $4}')</td><td>$(echo $t | awk '{print $5}')</td><td><a>删除</a></td></tr>"

        i=$((i + 1))
    done <template.txt

    trd_group=""

    mysql -hcm-mysql -uroot -p123.com cm-shell -e "select * from  hostgroup" | sed -e '1d' | grep -v "Warning" >hostgroup.txt
    q=0
    while read h; do
        Id=$(echo $h | awk '{print $1}')
        host_list=""
        Li=""

        trd_group+="<tr><td>$(echo $h | awk '{print $1}')</td><td>$(echo $h | awk '{print $2}')</td><td><a href='showContent.cgi?action=show_host_list&Id=$Id'>关联主机</a></td><td><a>删除</a></td></tr>"
    done <hostgroup.txt

    trd_host=""
    mysql -hcm-mysql -uroot -p123.com cm-shell -e "select * from  host" | sed -e '1d' | grep -v "Warning" >host.txt
    while read h; do

        trd_host+="<tr><td>$(echo $h | awk '{print $1}')</td><td>$(echo $h | awk '{print $2}')</td><td>$(echo $h | awk '{print $3}')</td><td>$(echo $h | awk '{print $4}')</td><td>$(echo $h | awk '{print $5}')</td><td>$(echo $h | awk '{print $7}')</td><td><a>删除</a></td></tr>"

    done <host.txt

    View=$(eval "cat <<EOF
    $(<template/hostgroup-home.html)
EOF
    ")

}

log() {
    REQUEST=$(base64 -d <<<$(echo $QUERY_STRING | awk -F 'data=' '{print $2}'))
    echo $(date +"%Y-%m-%e-%R"):[$1] "$2 $REQUEST_METHOD  $REQUEST $CONTENT_TYPE  $SCRIPT_NAME $PATH_INFO $SERVER_PROTOCOL" >>./log/$(date +"%Y-%m-%e").log

}

logInput() {
    echo $(date +"%Y-%m-%e-%R"):[$1] "$2" >>./log/$(date +"%Y-%m-%d").log

}

# getTable
# getGroup
